package guestBook;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	GuestBookMain gbm;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JTable table;
	private JButton btnSearch;
	private JTextField searchIn;

	public Select(GuestBookMain g) {
		super("방명록 조회",true,true,true,true);
		this.gbm=g;		
		setBounds(0, 449, 450, 300);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		
		setData();

	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnSearch(), BorderLayout.EAST);
			panel.add(getSearchIn(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(gbm.update == null){
						gbm.openUpdate();
					}
					gbm.update.setVisible(true);
					gbm.update.toFront();
					
					int row = table.getSelectedRow();
					int serial = Integer.parseInt((String)table.getValueAt(row, 0));
					String mid = (String)table.getValueAt(row, 1);
					String subject = (String)table.getValueAt(row, 2);
					
					gbm.update.select(serial);
					
					
					
				}
			});
		}
		return table;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setData();
				}
			});
		}
		return btnSearch;
	}
	private JTextField getSearchIn() {
		if (searchIn == null) {
			searchIn = new JTextField();
			searchIn.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == e.VK_ENTER){
						setData();
					}
				}
			});
			searchIn.setColumns(10);
		}
		return searchIn;
	}
	public void setData(){
		String sql = "select * from guestbook where mid=? or subject like ? or content like ?";
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		ResultSetMetaData rsmd=null;
		
		String find = searchIn.getText();
		
		try{
			pstmt = gbm.conn.prepareStatement(sql);
			pstmt.setString(1, find);
			pstmt.setString(2, "%"+find+"%");
			pstmt.setString(3, "%"+find+"%");
			rs = pstmt.executeQuery();
			
			// 테이블 컬럼명 추가
			rsmd = rs.getMetaData();
			Vector col = new Vector<>();
			for(int i = 1;i<=rsmd.getColumnCount();i++){
				col.add(rsmd.getColumnName(i));				
			}
			DefaultTableModel dtm = new DefaultTableModel(col,0);
			table.setModel(dtm);
			
			// 테이블 데이터 추가
			while(rs.next()){
				Vector rowdata = new Vector<>();
				for (int i = 1; i<=rsmd.getColumnCount(); i++){
					rowdata.add(rs.getString(i));
				}
				dtm.addRow(rowdata);
			}
			
			
			
			
		}catch(Exception ex){
			
		}		
	}
	
	
}
